package com.usian.login.service.impl;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.usian.common.ResultVo;
import com.usian.login.dao.LoginDao;
import com.usian.login.dao.UserDao;
import com.usian.login.pojo.TbUser;
import com.usian.login.service.LoginService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private LoginDao loginDao;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private UserDao userDao;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public ResultVo sendCode(String phone) throws Exception{
        //1.判读手机号是否为一个正确的手机号
        Boolean isPhone = Validator.isMobile(phone);
        if (isPhone) {
            //2.判断手机号是否注册
            int count = loginDao.getUserCountByPhone(phone);
            if (count > 0) {
                //手机号已注册
                //3.发送短信验证码
                rabbitTemplate.convertAndSend("java-exchange","java-queue",phone);
                return new ResultVo(200, "短信发送成功", null);
            } else {
                return new ResultVo(303, "手机号不存在或尚未激活", null);
            }

        } else {
            return new ResultVo(303, "手机号格式错误", null);
        }

    }

    @Override
    public ResultVo userLogin(String phone, String code) throws Exception{
        //1.判断手机号和验证码是否填写
        Boolean isHavephone = StrUtil.isEmpty(phone);
        Boolean isHaveCode = StringUtils.isEmpty(code);
        if (!isHavephone){
            if (!isHaveCode){
                //2.判断用户是否已经存在
                int count = loginDao.getUserCountByPhone(phone);
                if (count>0){
                    //3.验证码验证是否正确,匹配用户输入的验证码和我们保存的验证码是否一致
                    String redisCode = stringRedisTemplate.opsForValue().get(phone);
                    if (!StrUtil.isEmpty(redisCode)){
                        if (redisCode.equals(code)){
                            //4.返回用户个人信息
                            LambdaQueryWrapper<TbUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                            lambdaQueryWrapper.eq(TbUser::getMobile,phone);
                            TbUser tbUser = userDao.selectOne(lambdaQueryWrapper);
                            return new ResultVo(200,"验证码已发送",tbUser);
                        }else {
                            return new ResultVo(303,"验证码错误",null);
                        }
                    }else {
                        return new ResultVo(303,"验证码已超期",null);
                    }


                }else {
                    return new ResultVo(303,"此手机号不存在或尚未激活",null);
                }

            }else {
                return new ResultVo(303, "验证码必填", null);
            }
        }else {
            return new ResultVo(303, "手机号必填", null);
        }

    }

    @Override
    public ResultVo save(TbUser tbUser) {
        userDao.insert(tbUser);
        return new ResultVo(200, "success", null);
    }
}
